//
//  TradeMenuView.m
//  BTCoin
//
//  Created by Robin on 08/04/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "TradeMenuView.h"
#import "TradeCollectionViewCell.h"

typedef NS_ENUM(NSInteger,TradeType) {
    USDT_Type = 0,
    BTC_Type = 1,
    ETH_Type = 2
};

@interface TradeMenuView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,assign) TradeType tradeType;
@property (nonatomic,strong) UICollectionView * myCollectionView;
/** CollectionView的自带的流水布局 */
@property (nonatomic,strong) UICollectionViewFlowLayout * defauleLayout;
@property (nonatomic,strong) NSArray * dataArr;
@end


@implementation TradeMenuView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.tradeType = USDT_Type;
        
        UIView * headerView = [[UIView alloc] init];
        headerView.backgroundColor = UIColorFromRGB(0x00192A);
        [self addSubview:headerView];
        [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.trailing.equalTo(self);
            make.height.mas_equalTo(45.0);
        }];
        
        UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.backgroundColor = UIColorFromRGB(0x4B4C4D);
        backBtn.alpha = 0.7;
        [backBtn addTarget:self action:@selector(closeViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backBtn];
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(self);
        }];
        
        UILabel * USDTLabel = [[UILabel alloc] init];
        [USDTLabel setFont:[UIFont systemFontOfSize:17]];
        [USDTLabel setTextColor:COLOR_Bg_White];
        USDTLabel.text = NSLocalizedString(@"USDT交易", nil);
        [USDTLabel setTextAlignment:NSTextAlignmentCenter];
        [headerView addSubview:USDTLabel];
        [USDTLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(headerView);
            make.centerY.equalTo(headerView);
        }];
        
        UIButton * USDTBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        USDTBtn.tag = USDT_Type;
        USDTBtn.backgroundColor = [UIColor clearColor];
        [USDTBtn addTarget:self action:@selector(titleAction:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:USDTBtn];
        [USDTBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(USDTLabel);
        }];
        
        UILabel * BTCLabel = [[UILabel alloc] init];
        [BTCLabel setFont:[UIFont systemFontOfSize:17]];
        [BTCLabel setTextColor:COLOR_Bg_White];
        BTCLabel.text = NSLocalizedString(@"BTC交易", nil);
        [BTCLabel setTextAlignment:NSTextAlignmentCenter];
        [headerView addSubview:BTCLabel];
        [BTCLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(USDTLabel);
            make.leading.equalTo(USDTLabel.mas_trailing);
            make.width.equalTo(USDTLabel.mas_width);
        }];
        
        UIButton * BTCBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        BTCBtn.tag = BTC_Type;
        BTCBtn.backgroundColor = [UIColor clearColor];
        [BTCBtn addTarget:self action:@selector(titleAction:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:BTCBtn];
        [BTCBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(BTCLabel);
        }];
        
        UILabel * ETHLabel = [[UILabel alloc] init];
        [ETHLabel setFont:[UIFont systemFontOfSize:17]];
        [ETHLabel setTextColor:COLOR_Bg_White];
        ETHLabel.text = NSLocalizedString(@"ETH交易", nil);
        [ETHLabel setTextAlignment:NSTextAlignmentCenter];
        [headerView addSubview:ETHLabel];
        [ETHLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(USDTLabel);
            make.leading.equalTo(BTCLabel.mas_trailing);
            make.trailing.equalTo(headerView);
            make.width.equalTo(USDTLabel.mas_width);
        }];
        
        UIButton * ETHBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        ETHBtn.tag = ETH_Type;
        ETHBtn.backgroundColor = [UIColor clearColor];
        [ETHBtn addTarget:self action:@selector(titleAction:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:ETHBtn];
        [ETHBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(ETHLabel);
        }];
        
        _myCollectionView = [[UICollectionView alloc] initWithFrame:Rect(0, 45.0, RectWidth(frame), RectHeight(frame)-45.0-200) collectionViewLayout:self.defauleLayout];
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        _myCollectionView.showsVerticalScrollIndicator=YES;
        _myCollectionView.showsHorizontalScrollIndicator=NO;
        _myCollectionView.backgroundColor = kTableViewBgColor;
        //注册cell
        [_myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([TradeCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"TradeCollectionViewCell"];
        [self addSubview:self.myCollectionView];
        [_myCollectionView reloadData];
    }
    
    return self;
}

#pragma mark - event actions
- (void)titleAction:(UIButton *)sender {
    
}

- (void)closeViewAction:(UIButton *)sender {
    self.hidden = YES;
}

- (NSArray *)dataArr
{
    if(!_dataArr){
        _dataArr = @[@"USDT",@"LTC",@"ETH",@"ETC"];
    }
    return _dataArr;
}

#pragma mark - CollectionView的Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TradeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TradeCollectionViewCell" forIndexPath:indexPath];
    
    NSString * tStr = self.dataArr[indexPath.item];
    if (USDT_Type == self.tradeType) {
        tStr = [tStr stringByAppendingString:@"/USDT"];
    }
    
    cell.iconImageView.image = [UIImage imageNamed:@"order_btc_icon"];
    cell.nameLabel.text = tStr;
    
    return cell;
}

#pragma mark ----  点击item的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * rStr = @"";
    if (USDT_Type == self.tradeType) {
        rStr = @"USDT";
    }
    
    if ([self.delegate respondsToSelector:@selector(tradeType:region:)]) {
        [self.delegate tradeType:self.dataArr[indexPath.row] region:rStr];
    }
    
    self.hidden = YES;
}

#pragma mark - 懒加载
- (UICollectionView *)myCollectionView
{
    if(!_myCollectionView){
        /**
         注意：初始化collectionView 通过frame和layout 一定要传进去一个layout
         */
        
    }
    return _myCollectionView;
}

- (UICollectionViewFlowLayout *)defauleLayout
{
    if(!_defauleLayout){
        int column = 3;
        
        _defauleLayout=[[UICollectionViewFlowLayout alloc] init];
        _defauleLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _defauleLayout.sectionInset =UIEdgeInsetsMake(5.0, 0, 0, 0);
        
        float squareWidth = (RectWidth(self.frame)-5*(column-1))/column;
        _defauleLayout.itemSize = CGSizeMake((int)squareWidth, (int)squareWidth);
        _defauleLayout.minimumInteritemSpacing = 5.0;
        _defauleLayout.minimumLineSpacing = 5.0;
    }
    return _defauleLayout;
}

@end
